package com.itheima.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.goods.pojo.Goods;
import com.itheima.goods.service.GoodsService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author mySun
 * @create 2021-07-09 21:48
 */
@RestController
@RequestMapping("/goods")
//@CrossOrigin(allowCredentials = "true",allowedHeaders = "*",maxAge = 3600)
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @GetMapping
    public Result findAll(){
        List<Goods> goods = goodsService.list();
        return new Result<List<Goods>>(true, StatusCode.OK,"查询所有成功",goods);
    }

    @PostMapping
    public Result add(@RequestBody Goods goods){
        goodsService.save(goods);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    @GetMapping("/search/{page}/{size}")
    public Result findPage(@PathVariable("page")Integer curPage,@PathVariable("size")Integer size){
        Page<Goods> page = new Page<>(curPage,size);
        goodsService.page(page);

        PageResult<Goods> pageResult = new PageResult<>();
        pageResult.setTotal(page.getTotal());
        pageResult.setRows(page.getRecords());

        return new Result<>(true,StatusCode.OK,"分页查询成功",pageResult);
    }

    @PostMapping("/search/{page}/{size}")
    public Result findPageByMap(
            @PathVariable("page")Integer curPage,
            @PathVariable("size")Integer size,
            @RequestBody(required = false) Map<String,Object> searchMap){
        Page<Goods> page = new Page<>(curPage,size);
        LambdaQueryWrapper<Goods> lambdaQuery = Wrappers.lambdaQuery(Goods.class);
//        lambdaQuery
        if(searchMap != null){
            lambdaQuery.eq(ObjectUtils.isNotEmpty(searchMap.get("sn")),Goods::getSn,searchMap.get("sn"))
                    .like(ObjectUtils.isNotEmpty(searchMap.get("name")),Goods::getName,searchMap.get("name"))
                    .like(ObjectUtils.isNotEmpty(searchMap.get("caption")),Goods::getCaption,searchMap.get("caption"))
                    .eq(ObjectUtils.isNotEmpty(searchMap.get("price")),Goods::getPrice,searchMap.get("price"))
                    .eq(ObjectUtils.isNotEmpty(searchMap.get("num")),Goods::getNum,searchMap.get("num"))
                    .eq(ObjectUtils.isNotEmpty(searchMap.get("image")),Goods::getImage,searchMap.get("image"))
                    .eq(ObjectUtils.isNotEmpty(searchMap.get("images")),Goods::getImages,searchMap.get("images"))
                    .eq(ObjectUtils.isNotEmpty(searchMap.get("category")),Goods::getCategory,searchMap.get("category"))
                    .like(ObjectUtils.isNotEmpty(searchMap.get("brand")),Goods::getBrand,searchMap.get("brand"))
                    .like(ObjectUtils.isNotEmpty(searchMap.get("spec")),Goods::getSpec,searchMap.get("spec"))
                    .eq(ObjectUtils.isNotEmpty(searchMap.get("sqlNum")),Goods::getSaleNum,searchMap.get("sqlNum"))
                    .eq(ObjectUtils.isNotEmpty(searchMap.get("commentNum")),Goods::getCommentNum,searchMap.get("commentNum"))
                    .eq(ObjectUtils.isNotEmpty(searchMap.get("isMarketable")),Goods::getIsMarketable,searchMap.get("isMarketable"))
                    .eq(ObjectUtils.isNotEmpty(searchMap.get("status")),Goods::getStatus,searchMap.get("status"))
                    .eq(ObjectUtils.isNotEmpty(searchMap.get("createtime")),Goods::getCreatetime,searchMap.get("createtime"))
                    .eq(ObjectUtils.isNotEmpty(searchMap.get("updatetime")),Goods::getUpdatetime,searchMap.get("updatetime"));
        }

        goodsService.page(page,lambdaQuery);
        PageResult<Goods> pageResult = new PageResult<>();
        pageResult.setTotal(page.getTotal());
        pageResult.setRows(page.getRecords());

        return new Result<>(true,StatusCode.OK,"条件分页查询成功",pageResult);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id")Integer id){
        Goods goods = goodsService.getById(id);
        return new Result<>(true,StatusCode.OK,"查询成功",goods);
    }

    @PutMapping("/{id}")
    public Result update(@RequestBody Goods goods){
        goodsService.updateById(goods);
        return new Result<>(true,StatusCode.OK,"修改成功");
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        goodsService.removeById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }


}
